import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { NavController, IonInfiniteScroll, IonRefresher } from '@ionic/angular';
import { UrlService } from 'src/app/service/url.service';
import { HttpService } from 'src/app/service/http.service';
import { ToolService } from 'src/app/service/tool.service';
import { ConfigService } from 'src/app/service/config.service';
import { PopwinService } from 'src/app/service/popwin.service';


@Component({
  selector: 'app-reportlist',
  templateUrl: './reportlist.page.html',
  styleUrls: ['./reportlist.page.scss'],
})
export class ReportlistPage implements OnInit,AfterViewInit {
  private sdate:string;
  public dealDate="";//处理后的日期格式 2019-05-20为190520
  private pageNum = 0;  //页码
  public dataList: any[] = [];

  public isT=false;//是否第三方的显示数据
  public thirdList=[];
  public isAG=true;
  public isTag="";// 区分是利己SBO还是开元棋牌

  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  @ViewChild(IonRefresher) ionRefresher: IonRefresher;
  constructor(private activaRoute:ActivatedRoute,private navCtrl:NavController,private http:HttpService,private popWin:PopwinService) { }

  ngOnInit() {
    this.activaRoute.queryParams.subscribe((params:Params)=>{
      let tempDeal=JSON.parse(ToolService.paramAESDecrypt(params['p']));
      this.sdate=tempDeal['sdate'];
      this.dealDate=this.sdate.substr(2).replace(/-/g,'');
     if(tempDeal['t']==true || tempDeal['t']=="true"){
          this.isT=true;
          this.getThirdAllReportData(tempDeal['pid'],this.sdate);
          // if(tempDeal['pid']=='sbo'){
          //   this.getThirdReportSBOData(tempDeal['pid'],this.sdate);
          // }else if(tempDeal['pid']=='ky'){
          //   this.getThirdReportKYData(tempDeal['pid'],this.sdate);
          // }else if(tempDeal['pid']=='sg'){ 
          //   this.getThirdReportSGData(tempDeal['pid'],this.sdate);
          // }else if(tempDeal['pid']=='sw'){
          //   this.getThirdReportSWData(tempDeal['pid'],this.sdate);
          // }else if(tempDeal['pid']=='ps'){
          //   this.getThirdReportPSData(tempDeal['pid'],this.sdate);
          // }else{
          //   this.getThirdReportData(tempDeal['pid'],this.sdate);
          // }
      }
    });
  }
  ngAfterViewInit(){
    if(!this.isT){
      this.lementNet();
    }
   
  }
  lementNet(isMore?: boolean) {
    let that = this;
    let obj = "{\"is_hit\":\"" + 1 + "\",\"page\":\"" + this.pageNum + "\",\"pageSize\":\"20\",\"sdate\":\"" + this.sdate + "\"}";
    console.log(obj);
    this.http.get(UrlService.LOTSGAM_ORDERDETAIL, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      that.ionRefresher.complete();
      that.infiniteScroll.complete();
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let tt = ToolService.aesDecodeToJson(temp);
        if (isMore) {//上拉更多
          that.dataList = that.dataList.concat(tt.list);
          if (tt.list.length < 20) {
            that.infiniteScroll.disabled = true;
            return;
          }
        } else {//第一次进入
          that.dataList = tt.list;
          if (tt.list.length < 20) {
            that.infiniteScroll.disabled = true;
          } else {
            that.infiniteScroll.disabled = false;
          }
        }
        console.log(tt);
        //  console.log(tt);
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
      that.ionRefresher.complete();
      that.infiniteScroll.complete();
    });
  }
  /**
   * 下拉刷新
   */
  doRefresh(event) {
    this.pageNum = 0;
    this.lementNet();
  }
  /**
   * 上拉刷新
   */
  loadMoreData(event) {
    this.pageNum++;
    this.lementNet(true);
  }
  /**
   * 返回上一页
   */
  back(){
    this.navCtrl.back();
  }
  /**
   * 非万漉彩票的其他第三方报表详细
   * @param platformId 
   * @param date 
   */
  getThirdAllReportData(platformId,date){
    if(ToolService.isEmpty(platformId)){
      return;
    }
    let that=this;
    let obj = "{\"platform\":\"" + platformId + "\",\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_ALL_REPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("AG详细的数据列表============",res);
        that.thirdList=res;
        that.isAG=(platformId=="ag" || platformId=="1")?true:false;
        that.isTag=""+platformId;
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }
  /**
   * AG的报表详细列表
   */
  getThirdReportData(platformId,date){
    if(ToolService.isEmpty(platformId)){
      return;
    }
    let that=this;
    let obj = "{\"platformId\":\"" + platformId + "\",\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_REPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("AG详细的数据列表============",res);
        that.thirdList=res;
        that.isAG=true;
        that.isTag="";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }
  /**
   * 利己SBO的报表详细列表
   */
  getThirdReportSBOData(platformId,date){
    if(ToolService.isEmpty(platformId) || platformId!='sbo'){
      return;
    }
    let that=this;
    let obj = "{\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_SBOREPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("利己SBO的详细的数据列表===",res);
        that.thirdList=res;
        that.isAG=false;
        that.isTag="sbo";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  getThirdReportKYData(platformId,date){
    if(ToolService.isEmpty(platformId) || platformId!='ky'){
      return;
    }
    let that=this;
    let obj = "{\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_KYGAME_REPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("开元棋牌游戏的详细的数据列表===",res);
        that.thirdList=res;
        that.isAG=false;
        that.isTag="ky";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  getThirdReportSWData(platformId,date){
    if(ToolService.isEmpty(platformId) || platformId!='sw'){
      return;
    }
    let that=this;
    let obj = "{\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_SWGAME_REPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("双赢棋牌游戏的详细的数据列表===",res);
        that.thirdList=res;
        that.isAG=false;
        that.isTag="sw";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  getThirdReportPSData(platformId,date){
    if(ToolService.isEmpty(platformId) || platformId!='ps'){
      return;
    }
    let that=this;
    let obj = "{\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_PS_SBOREPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("平博体育的详细的数据列表===",res);
        that.thirdList=res;
        that.isAG=false;
        that.isTag="ps";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  getThirdReportSGData(platformId,date){
    if(ToolService.isEmpty(platformId) || platformId!='sg'){
      return;
    }
    let that=this;
    let obj = "{\"date\":\"" +date + "\"}";
    this.http.get(UrlService.THIRD_SGREPORTINFO, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let res = ToolService.aesDecodeToJson(data.data);
        console.log("双赢彩票游戏的详细的数据列表===",res);
        that.thirdList=res;
        that.isAG=false;
        that.isTag="sg";
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

}
